import {LOGIN, ROUTES, USER} from '@/services/api'
import {request, METHOD, removeAuthorization} from '@/utils/request'

/**
 * 登录服务
 * @param name 账户名
 * @param password 账户密码
 * @param key 验证码标识
 * @param code 验证码
 * @returns {Promise<AxiosResponse<T>>}
 */
export async function login(name, password, key, code) {
  return request(LOGIN, METHOD.POST, {
    account: name,
    password: password,
    key: key,
    code: code
  })
}

export async function getRoutesConfig() {
  return request(ROUTES, METHOD.GET)
}

/**
 * 退出登录
 */
export function logout() {
  localStorage.removeItem(process.env.VUE_APP_ROUTES_KEY)
  localStorage.removeItem(process.env.VUE_APP_PERMISSIONS_KEY)
  localStorage.removeItem(process.env.VUE_APP_ROLES_KEY)
  removeAuthorization()
}

export async function listUserByPage(currentPage, size) {
  return request(USER, METHOD.GET, {
    currentPage: currentPage,
    size: size
  })
}

export async function totalUser() {
  return request(USER+"/total", METHOD.GET)
}


export async function editUser(user) {
  return request(USER, METHOD.POST, {
    id: user.id,
    account: user.account,
    username: user.username,
    roleId: user.roleId,
    groupId: user.groupId
  })
}

export async function deleteUser(id) {
  return request(USER, METHOD.DELETE, {
    id: id
  })
}

export async function initPassword(id) {
  return request(USER+"/init-password", METHOD.GET, {
    id: id
  })
}

export async function updatePassword(old, now) {
  return request(USER+"/password", METHOD.POST, {
    old: old,
    now: now
  })
}

export async function findUser(account) {
  return request(USER+"/byAccount", METHOD.GET, {
    account: account
  })
}

export async function updateInfo(info) {
  return request(USER+"/info", METHOD.POST, {
    phone: info.phone,
    email: info.email
  })
}

export default {
  login,
  logout,
}
